<?php
/**
 * Plugin Toursprung: Allow display your Toursprung-Infos.
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Juergen A. Lamers <jaloma.ac@googlemail.com>
 */

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_toursprung extends DokuWiki_Syntax_Plugin {

//<iframe width='145' height='385' src='http://www.runmap.net/user/jaloma/user_info_widget' border='0' frameborder='0' marginheight='0' marginwidth='0' scrolling='no'></iframe>

  var $dflt = array(
		    'user' => '',
		    'site' => 'bikemap'
		    );
  
    /**
     * return some info
     */
  function getInfo(){
    return array(
		 'author' => 'Jürgen A. Lamers',
		 'email'  => 'jaloma.ac@googlemail.com',
		 'date'   => '2008-10-18',
		 'name'   => 'Toursprung Plugin',
		 'desc'   => 'Add your ToursprungInfos to your wiki
                              Syntax: <toursprung site="" user=""/>',
		 'url'    => 'http://jaloma.ac.googlepages.com/plugin:toursprung'
		 );
  }

    /**
     * What kind of syntax are we?
     */
  function getType() { return 'substition'; }
  function getPType() { return 'block'; }
    /**
     * Where to sort in to parse?
     */
  function getSort() { return 899; } 

    /**
    * Connect pattern to lexer
    */
  function connectTo($mode) { //     1234567890123
                              //     <reliwa
    $this->Lexer->addSpecialPattern('<toursprung ?[^>\n]*/>',$mode,'plugin_toursprung'); 
  }

  /*
  function postConnect() {
    $this->Lexer->addExitPattern('</toursprung>','plugin_toursprung');
  }*/

    /**
     * Handle the match
     */
  function handle($match, $state, $pos, &$handler){
    // break matched cdata into its components
    // 1234567890
    // toursprung
    list($str_params,$str_points) = explode('>',substr($match,11,-9),2);
    $gmap = $this->_extract_params($str_params);
    return array($gmap);
  }

    /**
     * Create output
     */
  function render($mode, &$renderer, $data) {
    if ($mode == 'xhtml') {
      list($param) = $data;

//<iframe width='145' height='385' src='http://www.runmap.net/user/jaloma/user_info_widget' border='0' frameborder='0' marginheight='0' marginwidth='0' scrolling='no'></iframe>
      $txt  = "";
      $txt .= '<span>';
      $txt .= '<iframe';
      $txt .= ' width="145"';
      $txt .= ' height="385"';
      $txt .= ' src="http://www.'.$param['site'].'.net/user/';
      $uname = $param['user'];
      if ($uname == '') {
		$uname = $this->getConf('user');
      }
      $txt .= $uname.'/user_info_widget"';
      $txt .= ' border="0"';
      $txt .= ' frameborder="0"';
      $txt .= ' marginheight="0"';
      $txt .= ' marginwidth="0"';
      $txt .= ' scrolling="no">';
      $txt .= '</iframe>';
      $txt .= '</span>';
      $renderer->doc .= $txt;
    }
    return false;
  }
  
  /**
   * extract parameters for the googlemap from the parameter string
   *
   * @param   string    $str_params   string of key="value" pairs
   * @return  array                   associative array of parameters key=>value
   */
  function _extract_params($str_params) {
    $param = array();
    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
    if (sizeof($param) == 0) {
      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
    }
    // parse match for instructions, break into key value pairs      
    $gmap = $this->dflt;
    foreach($param as $kvpair) {
      list($match,$key,$val) = $kvpair;
      $key = strtolower($key);
      if (isset($gmap[$key])) $gmap[$key] = $val;        
    }
    return $gmap;
  }
} /* CLASS */
?>